package org.example.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.example.system.entity.AssignedEntity;
import org.example.system.entity.CourseEntity;
import org.example.system.entity.StudentEntity;
import org.example.system.entity.TeacherEntity;
import org.example.system.mapper.AssignedMapper;
import org.example.system.service.AssignedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Arrays;
import java.util.Date;

/**
 * 排课服务实现
 */
@Service
public class AssignedServiceImpl implements AssignedService {
    @Autowired
    private AssignedMapper assignedMapper;

    /**
     * 按条件分页查询
     *
     * @param assigned
     * @return
     */
    @Override
    public IPage<AssignedEntity> selectPage(AssignedEntity assigned) {
        MPJLambdaWrapper<AssignedEntity> wrapper = new MPJLambdaWrapper<>();
        // 条件
        if (assigned.getStudentId() != null) {
            wrapper.eq(AssignedEntity::getStudentId, assigned.getStudentId());    // 相同的学生ID（精确查询）
        }
        if (assigned.getCourseId() != null) {
            wrapper.eq(AssignedEntity::getCourseId, assigned.getCourseId());    // 相同的课程ID（精确查询）
        }
        if (StrUtil.isNotEmpty(assigned.getCourseName())) {
            wrapper.like(CourseEntity::getName, assigned.getCourseName());
        }
        if (StrUtil.isNotEmpty(assigned.getTeacherName())) {
            wrapper.like(TeacherEntity::getUsername, assigned.getTeacherName());
        }
        if (StrUtil.isNotEmpty(assigned.getStudentName())) {
            wrapper.like(StudentEntity::getUsername, assigned.getStudentName());
        }
        if (StrUtil.isNotEmpty(assigned.getStudentMajor())) {
            wrapper.like(StudentEntity::getMajor, assigned.getStudentMajor());
        }
        //联查CourseEntity、TeacherEntity、StudentEntity
        wrapper.selectAll(AssignedEntity.class);
        wrapper.leftJoin(CourseEntity.class, CourseEntity::getId, AssignedEntity::getCourseId);
        wrapper.leftJoin(TeacherEntity.class, TeacherEntity::getId, CourseEntity::getTeacherId);
        wrapper.leftJoin(StudentEntity.class, StudentEntity::getId, AssignedEntity::getStudentId);
        wrapper.selectAs(CourseEntity::getName, "courseName");
        wrapper.selectAs(TeacherEntity::getUsername, "teacherName");
        wrapper.selectAs(StudentEntity::getFace, "face");
        wrapper.selectAs(StudentEntity::getNumber, "studentNum");
        wrapper.selectAs(StudentEntity::getUsername, "studentName");
        wrapper.selectAs(StudentEntity::getMajor, "studentMajor");

        IPage<AssignedEntity> page = new Page<>();
        page.setCurrent(assigned.getPageNum());
        page.setSize(assigned.getPageSize());
        return assignedMapper.selectPage(page, wrapper);
    }

    /**
     * 添加排课
     *
     * @param assigned
     * @return
     */
    @Override
    public int add(AssignedEntity assigned) {
        assigned.setCreateTime(new Date());
        return assignedMapper.insert(assigned);
    }

    /**
     * 修改
     *
     * @param assigned
     * @return
     */
    @Override
    public int update(AssignedEntity assigned) {
        assigned.setUpdateTime(new Date());
        return assignedMapper.updateById(assigned);
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(Long[] ids) {
        return assignedMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id获取信息
     *
     * @param id
     * @return
     */
    @Override
    public AssignedEntity getOne(Long id) {
        return assignedMapper.selectById(id);
    }
}
